package com.dh.project.controller;

import com.dh.project.model.EmployeeInfo;
import com.dh.project.model.PageInfo;
import com.dh.project.model.PageModel;
import com.dh.project.other.ModelRes;
import com.dh.project.service.EmployeeInfoService;
import com.dh.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;


/** 工人Controller
 * Created by admin on 2017/4/5.
 */
@Controller
public class EmployeeController {

    private EmployeeInfoService employeeInfoService;

    @Autowired
    public EmployeeController(EmployeeInfoService employeeInfoService){
        this.employeeInfoService = employeeInfoService;

    }

    /**
     * 工人信息列表
     * @param model
     * @return
     */
    @RequestMapping(value = "selectEmployeeList",produces = "text/html;charset=utf-8")
    public String selectEmployeeList(EmployeeInfo employee ,int page,Model model){
        Map map = new HashMap();
        map.put("employee_id",employee.getEmployee_id());
        map.put("employee_name",employee.getEmployee_name());

        map.put("page_start",(page-1)*10);
        map.put("page_end",10);

        List<EmployeeInfo> employeeInfoList = employeeInfoService.selectEmployeeInfoListByMap(map);

        PageModel pageModel = employeeInfoService.selectEmployeeInfoListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(employee,3));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);

        model.addAttribute("employeeList",employeeInfoList);
        model.addAttribute("employee",employee);
        return "employee_list";
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertEmployee",produces = "text/html;charset=utf-8")
    public Object insertEmployee(EmployeeInfo employee){
        if (employee == null){
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
        employee.setEmployee_status(1+"");
        employee.setCreatetime(String.valueOf(new Date().getTime()));
        employee.setModifytime(String.valueOf(new Date().getTime()));

        boolean isSuccess = employeeInfoService.insert(employee);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
    }

    /**
     * 修改工人信息
     * @param employee
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateEmployee",produces = "text/html;charset=utf-8")
    public Object updateEmployee(EmployeeInfo employee){
        if (employee.getEmployee_phone() != null){
            Map map = new HashMap();
            map.put("employee_phone",employee.getEmployee_phone());
            map.put("isDel",1);
            List<EmployeeInfo> employeeInfos = employeeInfoService.selectByMap(map);
            if (employeeInfos.size() != 0){
                if (employeeInfos.get(0).getEmployee_id() != employee.getEmployee_id()){
                    return new ModelRes(ModelRes.Status.ERROR,"此电话号码重复，修改失败!");
                }
            }
        }
        EmployeeInfo employeeInfo = employeeInfoService.selectById(employee.getEmployee_id());
//        if (admin.getAdmin_name() != null){
//            admin1.setAdmin_name(admin.getAdmin_name());
//        }
//        if (admin.getAdmin_phone() != null){
//            admin1.setAdmin_phone(admin.getAdmin_phone());
//        }
//        if (admin.getAdmin_login_name() != null){
//            admin1.setAdmin_login_name(admin.getAdmin_login_name());
//        }
//        if (admin.getAdmin_login_password() != null){
//            admin1.setAdmin_login_password(admin.getAdmin_login_password());
//        }
        boolean isSuccess = employeeInfoService.updateById(employeeInfo);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败!");
        }
    }

}
